/*
 * Copyright 2011 yura.
 *
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *      http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
package org.opu.db_vdumper.ui.component;

import java.awt.BorderLayout;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.event.ActionEvent;
import java.util.ArrayList;
import java.util.LinkedHashMap;
import java.util.List;
import java.util.Map;
import java.util.Set;
import javax.swing.Box;
import javax.swing.Icon;
import javax.swing.ImageIcon;
import javax.swing.JButton;
import javax.swing.JComponent;
import javax.swing.JFrame;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTable;
import javax.swing.JTextField;
import org.opu.db_vdumper.actions.AbstractAction;
import org.opu.db_vdumper.ui.i18n.PropertiesPanelI18N;
import org.opu.db_vdumper.ui.i18n.PropertiesPanelI18NImpl;
import org.opu.db_vdumper.util.ui.GUITextUtils;

/**
 *
 * @author yura
 */
public class PropertiesPanel extends JPanel {

    private Map<String, String> properties;
    private PropertiesPanelI18N i18n;
    private final JFrame frame;
    private final JButton show = new JButton(new AbstractActionImpl());

    public PropertiesPanel(Map<String, String> properties, PropertiesPanelI18N i18n) {
        super(new BorderLayout());
        show.setHideActionText(true);
        this.i18n = i18n;
        frame = new JFrame(this.i18n.getDialogTitle());

        Icon icon = show.getIcon();
        if (icon != null && icon instanceof ImageIcon) {
            final ImageIcon imgIcn = (ImageIcon) icon;
            frame.setIconImage(imgIcn.getImage());
        }

        setProperties(properties);
    }

    public PropertiesPanel(Map<String, String> properties) {
        this(properties, new PropertiesPanelI18NImpl());
    }

    public PropertiesPanel() {
        this(new LinkedHashMap<String, String>());
    }

    public Map<String, String> getProperties() {
        return properties;
    }

    public void setProperties(Map<String, String> properties) {
        this.properties = properties;
        reinit();
    }

    private void reinit() {
        removeAll();
        frame.dispose();

        Box w = Box.createVerticalBox();
        w.add(show);
        w.add(Box.createVerticalGlue());
        add(w, BorderLayout.WEST);

        Set<String> names = properties.keySet();
        String data[][] = new String[names.size()][2];
        List<JComponent> etalon = new ArrayList<JComponent>(data.length * 2);
        Box box = Box.createVerticalBox();
        int i = 0;
        for (String name : names) {
            String val = properties.get(name);
            String i18nName = i18n.getByKey(name);

            Box h = Box.createHorizontalBox();
            JLabel lbl = new JLabel(i18nName + ":", JLabel.RIGHT);
            h.add(lbl);
            h.add(Box.createHorizontalStrut(4));
            
            JTextField tf = GUITextUtils.addActions(new JTextField(val));
            tf.setEditable(false);
            Box v = Box.createVerticalBox();
            v.add(Box.createVerticalGlue());
            v.add(tf);
            v.add(Box.createVerticalGlue());
            h.add(v);

            box.add(h);
            box.add(Box.createVerticalStrut(4));

            data[i][0] = i18nName;
            data[i++][1] = val;

            etalon.add(lbl);
        }

        int width = 2;
        for (JComponent c : etalon) {
            if (c.getPreferredSize().width > width){
                width = c.getPreferredSize().width;
            }
        }

        for (JComponent c : etalon) {
            Dimension d = c.getPreferredSize();
            d.width = width;
            c.setMaximumSize(d);
            c.setMinimumSize(d);
            c.setPreferredSize(d);
        }
        
        add(box, BorderLayout.CENTER);
        
        JTable tbl = new JTable(data, new String[]{i18n.getNames(), i18n.getValues()});
        Container pane = frame.getContentPane();
        pane.removeAll();
        pane.add(new JScrollPane(tbl));

        revalidate();
        repaint();

    }

    class AbstractActionImpl extends AbstractAction {

        public AbstractActionImpl() {
            putValue(LARGE_ICON_KEY, null);
        }

        @Override
        public void action(ActionEvent e) {
            frame.setSize(540, 300);
            frame.setLocationRelativeTo(null);
            frame.setVisible(true);
        }

        @Override
        public String getName() {
            return "Show";
        }
    }

}
